Skip to content

Conversation

j4mie
Copy link
Member

@j4mie j4mie commented Nov 4, 2021

This PR adds a new (nullable) run_after field to the Job model, which is a DateTimeField. If this field is populated, the job worker will ignore any jobs for which run_after is in the future. This allows jobs to be scheduled to run later.

README.md Outdated
Job.objects.create(name='scheduled_job', run_after=timezone.now() + timedelta(minutes=10))
```

Of course, the job will only be run if your `python manage.py worker` process is running at the time when the job is scheduled to run. Otherwise, it will run the next time you start your worker process after that time has passed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it worth noting that this isn't exact? say if you're queue has a large backlog it may be sometime before the worker can pick it up

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's a good point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have added a note on that. A large backlog shouldn't be so much of an issue, because jobs are picked up in order of creation - so scheduled jobs were probably created a long time ago and so will be picked up before any other jobs that have been created more recently. However if a job is currently being processed then of course your scheduled job will only run after it's finished.

@j4mie j4mie merged commit 322f111 into master Nov 23, 2021
@j4mie j4mie deleted the run-after branch November 23, 2021 20:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants